package com.carpedil.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.carpedil.demo.dao.PermissionMapper;
import com.carpedil.demo.dao.UserDetailMapper;
import com.carpedil.demo.dao.UserMapper;
import com.carpedil.demo.dao.UserRoleMapper;
import com.carpedil.demo.po.Permission;
import com.carpedil.demo.po.Role;
import com.carpedil.demo.po.User;
import com.carpedil.demo.po.UserDetail;
import com.carpedil.demo.vo.UserVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/** @author Administrator */
@Service("userService")
public class UserServiceImpl implements UserService {
  @Autowired UserMapper userMapper;
  @Autowired UserDetailMapper udm;
  @Autowired UserRoleMapper urm;
  @Autowired PermissionMapper pm;

  @Override
  public User login(User user) {
    User u =
        userMapper.selectOne(
            new QueryWrapper<User>()
                .eq("user_name", user.getUserName())
                .eq("pass_word", user.getPassWord()));
    if (u == null) {
      return null;
    }
    return getUserData(u);
  }

  @Override
  public User login2(User user) {
    return userMapper.login2(
        new QueryWrapper<User>()
            .eq("user_name", user.getUserName())
            .eq("pass_word", user.getPassWord()));
  }

  @Override
  public List<UserVo> findAllUsers() {
    List<User> users = userMapper.selectList(null);
    for (User user : users) {
      user = getUserData(user);
    }
    return getVoList(users);
  }

  @Override
  public User getUserById(int userId) {
    User user = userMapper.selectById(userId);
    return getUserData(user);
  }

  private User getUserData(User user) {
    // detail
    UserDetail userDetail =
        udm.selectOne(
            new QueryWrapper<UserDetail>().eq("user_detail_id", user.getFkUserDetailId()));
    user.setUserDetail(userDetail);
    // roles
    List<Role> roles = urm.getRolesById(user.getId());
    user.setRoles(roles);
    // 获取权限
    List<Permission> permissions = pm.getUserPermissions(user.getId());
    // 获取子权限
    for (Permission permission : permissions) {
      List<Permission> childPerList =
          pm.selectList(new QueryWrapper<Permission>().eq("pid", permission.getPermissionPkId()));

      permission.setChildPer(childPerList);
    }
    user.setPermissions(permissions);
    return user;
  }

  private List<UserVo> getVoList(List<User> users) {
    List<UserVo> voList = new ArrayList<>();
    for (User user : users) {
      UserVo vo = new UserVo();
      try {
        BeanUtils.copyProperties(vo, user);
        BeanUtils.copyProperty(vo, "userDetailId", user.getUserDetail().getUserDetailId());
        BeanUtils.copyProperty(vo, "userTel", user.getUserDetail().getUserTel());
        BeanUtils.copyProperty(vo, "userAddress", user.getUserDetail().getUserAddress());
        BeanUtils.copyProperty(vo, "userImg", user.getUserDetail().getUserImg());
        List<Role> roles = user.getRoles();
        StringBuilder roleName = new StringBuilder();
        for (Role role : roles) {
          roleName.append(role.getRoleName()).append(" ");
        }
        BeanUtils.copyProperty(vo, "roleName", roleName.toString());

      } catch (IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
      }
      voList.add(vo);
    }
    return voList;
  }
}
